package com.apartment.own.controller;

import java.util.List;
import java.util.Map;

import com.apartment.common.core.domain.entity.SysUser;
import com.apartment.common.core.domain.model.LoginUser;
import com.apartment.common.utils.ServletUtils;
import com.apartment.common.utils.spring.SpringUtils;
import com.apartment.framework.web.service.TokenService;
import com.apartment.own.request.sys.NotificationMessageDTO;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.aspectj.lang.annotation.AdviceName;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.apartment.common.annotation.Log;
import com.apartment.common.core.controller.BaseController;
import com.apartment.common.core.domain.AjaxResult;
import com.apartment.common.enums.BusinessType;
import com.apartment.own.domain.OwnSystemNotificationMessage;
import com.apartment.own.service.IOwnSystemNotificationMessageService;
import com.apartment.common.utils.poi.ExcelUtil;
import com.apartment.common.core.page.TableDataInfo;
import springfox.documentation.annotations.ApiIgnore;

/**
 * 后台消息通知Controller
 *
 * @author @Dog_E
 * @date 2020-08-28
 */
@Api(value = "后台消息通知Controller", tags = "后台消息通知")
@RestController
@RequestMapping("/own/message")
public class OwnSystemNotificationMessageController extends BaseController {
    @Autowired
    private IOwnSystemNotificationMessageService ownSystemNotificationMessageService;

//    /**
//     * 查询后台消息通知列表
//     */
//    @PreAuthorize("@ss.hasPermi('own:message:list')")
//    @GetMapping("/list")
//    public TableDataInfo list(OwnSystemNotificationMessage ownSystemNotificationMessage)
//    {
//        startPage();
//        List<OwnSystemNotificationMessage> list = ownSystemNotificationMessageService.selectOwnSystemNotificationMessageList(ownSystemNotificationMessage);
//        return getDataTable(list);
//    }

    /**
     * 导出后台消息通知列表
     */
    @PreAuthorize("@ss.hasPermi('own:message:export')")
    @Log(title = "后台消息通知", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(OwnSystemNotificationMessage ownSystemNotificationMessage) {
        List<OwnSystemNotificationMessage> list = ownSystemNotificationMessageService.selectOwnSystemNotificationMessageList(ownSystemNotificationMessage);
        ExcelUtil<OwnSystemNotificationMessage> util = new ExcelUtil<OwnSystemNotificationMessage>(OwnSystemNotificationMessage.class);
        return util.exportExcel(list, "message");
    }

    /**
     * 获取后台消息通知详细信息
     */
    @PreAuthorize("@ss.hasPermi('own:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(ownSystemNotificationMessageService.selectOwnSystemNotificationMessageById(id));
    }

    /**
     * 新增后台消息通知
     */
    @PreAuthorize("@ss.hasPermi('own:message:add')")
    @Log(title = "后台消息通知", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OwnSystemNotificationMessage ownSystemNotificationMessage) {
        return toAjax(ownSystemNotificationMessageService.insertOwnSystemNotificationMessage(ownSystemNotificationMessage));
    }

    /**
     * 修改后台消息通知
     */
    @PreAuthorize("@ss.hasPermi('own:message:edit')")
    @Log(title = "后台消息通知", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OwnSystemNotificationMessage ownSystemNotificationMessage) {
        return toAjax(ownSystemNotificationMessageService.updateOwnSystemNotificationMessage(ownSystemNotificationMessage));
    }

    /**
     * 删除后台消息通知
     */
    @PreAuthorize("@ss.hasPermi('own:message:remove')")
    @Log(title = "后台消息通知", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(ownSystemNotificationMessageService.deleteOwnSystemNotificationMessageByIds(ids));
    }


    /**
     * @param messageDTO: 查询消息通知DTO
     * @Description: 查询后台消息通知列表
     * @return: com.apartment.common.core.page.TableDataInfo
     * @Author: @Dog_E
     * @Date: 2020/9/5 0005 10:30
     **/
    @PreAuthorize("@ss.hasPermi('own:message:list')")
    @GetMapping("/list")
    @ApiOperation("查询消息通知")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "messageStatus", value = "消息状态 0:未读 1:已读", required = true),
            @ApiImplicitParam(name = "messageType", value = "消息类型 ps:查询枚举ENUM_SystemNotificationTypeEnum", required = true),
    })
    public TableDataInfo list(@ApiIgnore NotificationMessageDTO messageDTO) {
        startPage();
        List<OwnSystemNotificationMessage> messages = ownSystemNotificationMessageService.selectMessageList(messageDTO, TokenService.getUser());
        return getDataTable(messages);
    }

    /**
     * @param ids: 消息id
     * @Description: 增加已读标识
     * @Author: @Dog_E
     * @Date: 2020/9/5 0005 11:38
     **/
    @PreAuthorize("@ss.hasPermi('own:message:edit')")
    @PutMapping("increaseTheReadMark/{ids}")
    @ApiOperation("增加已读标识")
    @ApiImplicitParam(name = "ids", dataType = "String", format = "数组")
    public AjaxResult increaseTheReadMark(@PathVariable Long[] ids) {
        return AjaxResult.success(ownSystemNotificationMessageService.increaseTheReadMark(ids, TokenService.getUser().getUserId()));
    }

    /**
     * @Description: 查询未读条数
     * @Author: @Dog_E
     * @Date: 2020/9/7 0007 18:33
     **/
    @PreAuthorize("@ss.hasPermi('own:message:list')")
    @GetMapping("/inquireUnreadNumber")
    @ApiOperation("查询未读条数")
    public AjaxResult inquireUnreadNumber() {
        Map<String, Integer> map = ownSystemNotificationMessageService.inquireUnreadNumber();
        return AjaxResult.success(map);
    }


}
